﻿using ViewMaker.Core.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ViewMaker.Core;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;

namespace ViewMaker.Core.Tests
{

    [TestClass()]
    public class ComboBoxViewControlTest
    {
        [TestMethod()]
        public void 標準的なComboBoxのXAML生成()
        {
            var target = new ComboBoxViewControl { Item = new ViewLayoutItem { Name = "cb001", Caption = "コンボボックス", BindPath = "currentValue" } };
            target.ItemsSource = "dataSource";
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[1];

            var actual = TestHelper.GetFrameworkElement<ComboBox>(xaml);
            Assert.AreEqual("cb001", actual.Name);
            Assert.AreEqual(false, actual.IsEditable, "IsEditable");
            Assert.AreEqual("currentValue", actual.GetBindingExpression(ComboBox.SelectedItemProperty).ParentBinding.Path.Path);
            Assert.AreEqual("dataSource", actual.GetBindingExpression(ComboBox.ItemsSourceProperty).ParentBinding.Path.Path);
            Assert.AreEqual(BindingMode.TwoWay, actual.GetBindingExpression(ComboBox.SelectedItemProperty).ParentBinding.Mode);
        }

        [TestMethod()]
        public void 編集可能なComboBoxのXAML生成()
        {
            var target = new ComboBoxViewControl { Item = new ViewLayoutItem { Name = "cb001", Caption = "コンボボックス", BindPath = "currentValue" } };
            target.ItemsSource = "dataSource";
            target.IsEditable = true;
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[1];

#if !SILVERLIGHT
            var actual = TestHelper.GetFrameworkElement<ComboBox>(xaml);
            Assert.AreEqual("cb001", actual.Name);
            Assert.AreEqual(true, actual.IsEditable, "IsEditable");
            Assert.AreEqual("currentValue", actual.GetBindingExpression(ComboBox.TextProperty).ParentBinding.Path.Path);
            Assert.AreEqual("dataSource", actual.GetBindingExpression(ComboBox.ItemsSourceProperty).ParentBinding.Path.Path);
            Assert.AreEqual(BindingMode.TwoWay, actual.GetBindingExpression(ComboBox.TextProperty).ParentBinding.Mode);
#else
            var actual = TestHelper.GetFrameworkElement<AutoCompleteBox>(xaml);
            Assert.AreEqual("cb001", actual.Name);
            Assert.AreEqual("currentValue", actual.GetBindingExpression(AutoCompleteBox.TextProperty).ParentBinding.Path.Path);
            Assert.AreEqual(BindingMode.TwoWay, actual.GetBindingExpression(AutoCompleteBox.TextProperty).ParentBinding.Mode);
#endif
        }
        [TestMethod()]
        public void 値表示カラム指定なComboBoxのXAML生成()
        {
            var target = new ComboBoxViewControl { Item = new ViewLayoutItem { Name = "cb001", Caption = "コンボボックス", BindPath = "currentValue" } };
            target.ItemsSource = "dataSource";
            target.DisplayMemberPath = "display";
            target.SelectedValuePath = "valuePath";
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[1];

            var actual = TestHelper.GetFrameworkElement<ComboBox>(xaml);
            Assert.AreEqual("cb001", actual.Name);
            Assert.AreEqual("currentValue", actual.GetBindingExpression(ComboBox.SelectedValueProperty).ParentBinding.Path.Path);
            Assert.AreEqual("dataSource", actual.GetBindingExpression(ComboBox.ItemsSourceProperty).ParentBinding.Path.Path);
            Assert.AreEqual(BindingMode.TwoWay, actual.GetBindingExpression(ComboBox.SelectedValueProperty).ParentBinding.Mode);
            Assert.AreEqual("display", actual.DisplayMemberPath);
            Assert.AreEqual("valuePath", actual.SelectedValuePath);
        }

        [TestMethod()]
        public void アイテム指定なComboBoxのXAML生成()
        {
            var target = new ComboBoxViewControl { Item = new ViewLayoutItem { Name = "cb001", Caption = "コンボボックス", BindPath = "currentValue" } };
            target.Items = new string[] { "ONE", "TWO", "THREE" };
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[1];

            var actual = TestHelper.GetFrameworkElement<ComboBox>(xaml);
            Assert.AreEqual("cb001", actual.Name);
            Assert.AreEqual("currentValue", actual.GetBindingExpression(ComboBox.SelectedItemProperty).ParentBinding.Path.Path);
            Assert.AreEqual(BindingMode.TwoWay, actual.GetBindingExpression(ComboBox.SelectedItemProperty).ParentBinding.Mode);
            Assert.AreEqual("ONE", (string)actual.Items[0]);
            Assert.AreEqual("THREE", (string)actual.Items[2]); 

        }

        [TestMethod()]
        public void ComboBoxViewControlの編集画面が表示可能()
        {
            var target = new ComboBoxViewControl { Item = new ViewLayoutItem { Name = "cb001", Caption = "コンボボックス", BindPath = "currentValue" } };
            target.ItemsSource = "dataSource";
            var baml = ViewUtil.BuildContent(target);
            Assert.IsNotNull(baml);
        }


    }
}
